<?php
/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

namespace org\boapi\shop;

include_once (__DIR__ . '/ISBN.php');

/**
 * International Standard Book Number [ISBN] before 01.01.2007.
 * @author Sebastian Ritter
 * @version 1.0.0.0  
 * @license Apache License Version 2.0
 */
final class ISBN10 extends \org\boapi\shop\ISBN {

  /**
   * Is the checknumber correct
   * @return true, if checknumber is correct 
   */
  public function isChecknumberCorrect () {
    $factors = array ( 10,9,8, 7, 6, 5, 4, 3, 2 ); // only first seven
    $digits = array();
    for ($i = 0; $i < count ( $factors ); $i++) {
      $digits[$i] = substr ($this->getNumber(),$i, 1);
    }
    $sum = 0;
    for ($i = 0; $i < count($digits); $i++) {
      $sum += $digits[$i] * $factors[$i];
    }
    $mod11 = sum % 11;
    $checkDigit = 11 - mod11;
    switch ($checkDigit) {
      case 10:
        $this->setChecknumber('X');
        break;
      case 11:
        $this->setChecknumber('0');
        break;
      default:
        $this->setChecknumber(substr($checkDigit,0,1));
        break;
    }
    return $this->getChecknumber() == substr ($this->getNumber(),9,1);
  }
}
?>